<template>
  <div class="app-container">
    <el-card>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="封面图片" prop="cover">
          <oss class="cover" v-model="form.cover" :limit="1" ossURL="/oss/uploadResourcePic" />
        </el-form-item>
        <el-form-item label="文章标题" prop="newsTitle">
          <el-input maxlength="50" v-model="form.newsTitle" placeholder="请输入文章标题" />
        </el-form-item>
        <el-form-item label="文章说明" prop="newsDiscription">
          <el-input type="textarea" maxlength="60" show-word-limit v-model="form.newsDiscription"
            placeholder="请输入文章说明" />
        </el-form-item>
        <el-form-item label="文章内容" prop="content">
          <editor v-model="form.content" height="500px" />
        </el-form-item>
        <el-form-item label="启用状态" prop="enabled">
          <el-switch v-model="form.enabled" :active-value="1" :inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { updateResources } from '@/api/resource/resources';
import rules from '@/rules/resource'
export default {
  data() {
    return {
      form: {},
      rules: rules
    }
  },
  components: {
    oss: () => import("@/components/ImageUpload/oss.vue"),
  },
  created() {
    let info = sessionStorage.getItem('resource');
    if (!info) {
      return;
    }
    this.form = JSON.parse(info);
  },
  methods: {
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        updateResources(this.form).then((response) => {
          this.$modal.msgSuccess('新增成功');
          this.cancel();
        })
      })
    },
    cancel() {
      this.$router.push({ path: '/resources/' });
    }
  }
}
</script>

<style scoped>
.cover>>>.el-upload-dragger {
  width: 150px;
  height: 150px;
}
</style>
